import { useSessionStorageState } from "ahooks";
import { message } from "antd";
import { createModel } from "hox";
import { ResponseTypes, usePostRequest } from "../http";
import { loginByPassword, loginByMobile, sendValidCode } from "../http/apis/login-api";

export interface CustManager {
    custManagerName: string
    departmentName: string
    mobile: string
}

export interface Jsession {
    buttonPermissions: Array<string>
    menuResource: Array<string | null>
    token: string
    username: string
}

export interface UserInfo {
    custManager: CustManager
    jsession: Jsession
    realName: string
}

export interface User {
    realName: string
    username: string
    custManagerName: string
    departmentName: string
    mobile: string
}

export interface Permissions {
    buttonPermissions: Array<string>
    menuResource: Array<string | null>
}

type PasswordLoginParams = {
    username: string,
    password: string
}

type MobileLoginParams = {
    mobile: string,
    code: string
}

type SendValidCodeParams = {
    mobile: string
}

const useLogin = () => {

    const [user, setUser] = useSessionStorageState<User>('user')
    const [token, setToken] = useSessionStorageState<string>('token')
    const [permissions, setPermissions] = useSessionStorageState<Permissions>('permissions')

    const successAction = (data: ResponseTypes) => {
        message.success('登录成功')
        const result: UserInfo = data.content || {}
        setUser({
            realName: result.realName,
            username: result.jsession.username,
            custManagerName: result.custManager.custManagerName,
            departmentName: result.custManager.departmentName,
            mobile: result.custManager.mobile
        })
        setToken(result.jsession.token)
        setPermissions({
            buttonPermissions: result.jsession.buttonPermissions,
            menuResource: result.jsession.menuResource
        })
    }

    // 密码登录
    const passwordLoginParams = { url: loginByPassword, successAction }
    const passwordLoginRequest = usePostRequest(passwordLoginParams)
    const passwordLogin = (params: PasswordLoginParams) => passwordLoginRequest.run(params)
    const passwordLoading = passwordLoginRequest.loading
    const passwordData = passwordLoginRequest.data
    //验证码登录
    const mobileLoginParams = { url: loginByMobile, successAction }
    const mobileLoginRequest = usePostRequest(mobileLoginParams)
    const mobileLogin = (params: MobileLoginParams) => mobileLoginRequest.run(params)
    const mobileLoading = mobileLoginRequest.loading
    const mobileData = mobileLoginRequest.data
    //获取验证码
    const sendValidCodeRequest = usePostRequest({ url: sendValidCode, successAction: '获取验证码成功' })
    const send = (params: SendValidCodeParams) => sendValidCodeRequest.run(params)
    const codeLoading = sendValidCodeRequest.loading
    //登出系统清空用户信息
    const logout = () => {
        setUser()
        setToken()
        setPermissions()
    }

    return {
        user,
        token,
        permissions,
        passwordLoading,
        passwordData,
        passwordLogin,
        mobileLogin,
        mobileData,
        mobileLoading,
        codeLoading,
        send,
        logout
    }
}

export default createModel(useLogin)